package pe.gob.onpe.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.naming.InitialContext;


public class ServiceLocator {
	InitialContext ctx = null;
	
	private static ServiceLocator locator;
	
	static{
		try{
			locator = new ServiceLocator();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public ServiceLocator(){}
	
	public Object getLocal(String jndiName){
		try{
			ctx = new InitialContext();
			Object objref = ctx.lookup(jndiName);
			return objref;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Object getRemote(String jndiName) {
		try{
			ctx = new InitialContext(getPropiedades());
			Object objref = ctx.lookup(jndiName);
			return objref;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private Properties getPropiedades() throws FileNotFoundException, IOException{
		Properties propiedades = new Properties();
		FileInputStream archivo = new FileInputStream(ServiceLocator.class.getResource("jndi.properties").getFile());
		propiedades.load(archivo);
		return propiedades;
	}
	
	static public ServiceLocator getInstance() {
		return locator;
	}
}
